define(['amazeui', 'jquery', 'global/url', 'global/constants'],
  function(amazeui, jquery, url, constants) {
    return ['$scope', '$rootScope', 'UrlService', 'AdminService', 'UtilService', 'ParentService',
    		 function($scope, $rootScope, UrlService, AdminService, UtilService, ParentService) {
        $scope.awesomeThings = [
	      'HTML5 Boilerplate',
	      'AngularJS',
	      'Karma'
        ];
        $('#my-end-2').datepicker({
              onRender: function(date) {
              return  '' ;
              }
        });


	    var params = {
                'userId': 0,
               'accessToken': $scope.accessToken,
               'filter': {
                  'pagesize': 10,
                  'pno': 1,
                 'search': ''
             }
        } 
        
        $scope.remove = function(parent) {
           if(confirm("Are you sure to delete")){
                var param = {
                'userId':parent.userId,
                };
                ParentService.parentDelete(param).success(function(response, status) {
                    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
                        _refreshParent(); 
                    }   
                }); 
 
             }
        }
        $scope.setUserId = function(parent){
          $scope.id = parent.userId;
        }
        $scope.addChild = function(){
          var param = {
            'accessToken': $scope.accessToken,
            'name': $scope.name, 
            'childGender': $scope.childGender, 
            'iconName':$scope.accessToken,
            'age':$scope.age,
            'birthday':$scope.birthday,
            'parentId':$scope.id
          }; 
          UtilService.validateForm('#child-modal');
            if($('#child-modal').data('amui.validator').isFormValid()) {
               ParentService.parentChildAdd(param).success(function(response, status) {
                    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
                      $('.am-close-spin').click();
                        _refreshParent(); 
                    }   
                }); 
            }; 
        }

        function _refreshParent () {
            params.filter.pagesize = $scope.parents.pagesize;
            params.filter.pno = $scope.parents.currentPage;
             ParentService.parentList(params).success(function(response, status) {
                if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
                  $scope.parents = response.jsonResponse.response.data;
                } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
                    AdminService.Admin.logout();
                   window.location.href = constants.pages.login;
                }
            });
        }
        function _getParentList (params) {
            ParentService.parentList(params).success(function(response, status) {
                if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
                   $scope.parents = response.jsonResponse.response.data;
                } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
                   AdminService.Admin.logout();
                   window.location.href = constants.pages.login;
                }
            });
        }
         UtilService.renderSelect();
        _getParentList(params);
       

     }];
  }
)